package com.xtpt.disaster.controller;


import com.xtpt.disaster.bean.NodeInfo;
import com.xtpt.disaster.common.PortalResult;
import com.xtpt.disaster.service.NodeInfoManageService;
import com.xtpt.disaster.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping(value = "/manage/nodeinfo")
public class NodeInfoManageController {
    @Autowired
    NodeInfoManageService nodeInfoManageService;

    @PostMapping(value = "/insert")
    public PortalResult insertNodeInfo(@RequestBody NodeInfo nodeInfo) throws ParseException {
        int res = nodeInfoManageService.insertNodeInfoSelective(nodeInfo);
        if (res == 0) {
            return PortalResult.error("fasle");
        }
        else {
            return PortalResult.ok(res);
        }
    }

    @PostMapping(value = "/batch/delete")
    public PortalResult deleteNodeInfoBatch(@RequestBody long[] nodeIdList){
        int res = nodeInfoManageService.deleteBatch(nodeIdList);
        LogUtil.logger.info(String.valueOf(res));
        if (res == 0) {
            return PortalResult.error("fasle");
        }
        else {
            return PortalResult.ok(res);
        }
    }

    @DeleteMapping(value = "/delete")
    public PortalResult deleteNodeInfo(@RequestParam(value = "id") long id){
        int res = nodeInfoManageService.deleteById(id);
        if(res == 0) {
            return PortalResult.error("false");
        }
        else {
            return PortalResult.ok(res);
        }
    }

    @PostMapping(value = "/update")
    public PortalResult updateNodeinfo(@RequestBody NodeInfo nodeInfo) {
        int res = nodeInfoManageService.updateByIdSelective(nodeInfo);
        if (res == 0) {
            return PortalResult.error("fasle");
        }
        else {
            return PortalResult.ok(res);
        }
    }

    @GetMapping(value = "/getbyid")
    public PortalResult getNodeinfoById(@RequestParam(value = "id") long id){
        NodeInfo data = nodeInfoManageService.selectById(id);
        return PortalResult.ok(data);
    }

    @GetMapping(value = "/getbyip")
    public PortalResult getNodeInfoByIp(@RequestParam(value = "ip") String ip){
        List<NodeInfo> data = nodeInfoManageService.selectByIp(ip);
        return PortalResult.ok(data);
    }

    @GetMapping(value = "/getall")
    public PortalResult getAllNodeInfo() {
        List<NodeInfo> data = nodeInfoManageService.selectAll();
        //LogUtil.logger.info(data.get(0).getNodeId().toString());
        return PortalResult.ok(data);
    }
}
